const app = getApp();

Page({
  data: {
    info : {},
    imgArr:{},
    phone : '15521211667',
    goods_id:0,
    id:0,
    cartNum:0,
    aid:0,
    token:''
  },
  onBlur(e) {
    this.setData({
      inputValue: e.detail.value,
    });
  },
  onShareAppMessage() {
    return {
      title: this.data.info.goods_name,
      desc: this.data.info.goods_name,
      path: 'pages/add-todo/detail?id='+this.data.id
    };
  },
  onShow(){
    var that = this;
    my.getStorage({
      key: 'token',
      success: function(res) {  
        if(typeof res.data!='undefined'){
          that.countCart(res.data.token);
        }
      }
    });
  },
  onLoad(res) {
    var id = res.id;
    var that = this;
    my.setNavigationBar({
      title: '商品详情',
    });
    my.getStorage({
      key: 'token',
      success: function(res) {  
        if(typeof res.data!='undefined'){
          that.countCart(res.data.token);
        }
      }
    });
    my.removeStorage({
      key: 'address'
    });
    my.httpRequest({
      url: app.geturl+'/feedback/get-tel',
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        that.setData({
          phone: res.data.data.tel
        });
      }
    });
    my.httpRequest({
      url: app.geturl+'/goods-common/info',
      method: 'GET',
      data: {
        goods_common_id: res.id
      },
      dataType: 'json',
      success: function(res) {
        console.info(res);
        var str = res.data.data.Obj.body;
        var imgReg = /<img.*?(?:>|\/>)/gi;
        //匹配src属性
        var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
        var arr = str.match(imgReg);
        var imgArr = Array();
        if(arr!=null){
          for (var i = 0; i < arr.length; i++) {
            var src = arr[i].match(srcReg);
            //获取图片地址
            if(src[1]){
              imgArr[i] = src[1];
            }
          }
        }
        that.setData({
          info: res.data.data.Obj,
          imgArr:imgArr,
          goods_id:res.data.data.goodsList[0].goods_id,
          id:id
        });
      }
    });
  },
  makePhoneCall(e) {
    my.makePhoneCall({ number: e.target.dataset.phone });
  },
  cartAdd(){
    my.showLoading({});
    var that = this;
    my.getAuthCode({
      scopes: ['auth_user'],
      success: (authcode) => {
        my.httpRequest({
          url: app.geturl+'/member/login-by-ali-sp',
          method: 'POST',
          data:{
            code:authcode.authCode
          },
          dataType: 'json',
          success: function(res) {
            console.info(res);
            if(res.data.msg=='登录成功'){                    
              that.setData({
                token:res.data.data.token
              })
              my.setStorage({
                key: 'token',
                data: {
                  token:res.data.data.token,
                  time:600
                },
                success: function() {
                  my.hideLoading();
                  my.getAuthUserInfo({
                    success: (r) => {
                      my.setStorage({
                        key: 'memberInfo',
                        data: {
                          avatar:r.avatar,
                          nickName:r.nickName,
                        }
                      });
                    }
                  });
                  that.cartAddSub(res.data.data.token);
                }
              });
            }else{
              my.hideLoading();
              my.removeStorage({
                key: 'token',
                success: function(){
                  my.alert({
                    content: '授权登录失败',
                  });
                }
              });
            }
          }
        });
      }
    });
    my.hideLoading();
  },
  cartAddSub(token){
    var that = this;
    my.httpRequest({
      url: app.geturl+'/cart/add',
      method: 'GET',
      data: {
        token:token,
        goods_list:that.data.goods_id+':1'
      },
      dataType: 'json',
      success: function(res) {
        console.info(res);
        that.countCart(token);
      }
    }); 
  },
  countCart(token){
    var that = this;
    my.httpRequest({
      url: app.geturl+'/cart/num',
      method: 'GET',
      data: {
        token:token
      },
      dataType: 'json',
      success: function(res) {
        console.info(res);
        that.setData({
          cartNum:res.data.data
        });
      }
    }); 
  },
  add() {
    app.todos = app.todos.concat([{
      text: this.data.inputValue,
      compeleted: false,
    }]);

    my.navigateBack();
  },
  cartList(){
    my.showLoading({});
    my.getAuthCode({
      scopes: ['auth_user'],
      success: (authcode) => {
        my.httpRequest({
          url: app.geturl+'/member/login-by-ali-sp',
          method: 'POST',
          data:{
            code:authcode.authCode
          },
          dataType: 'json',
          success: function(res) {
            if(res.data.msg=='登录成功'){
              my.setStorage({
                key: 'token',
                data: {
                  token:res.data.data.token,
                  time:600
                },
                success: function() {
                  my.hideLoading();
                  my.getAuthUserInfo({
                    success: (r) => {
                      my.setStorage({
                        key: 'memberInfo',
                        data: {
                          avatar:r.avatar,
                          nickName:r.nickName,
                        }
                      });
                    }
                  });
                  my.navigateTo({ url: '../add-todo/cart' });
                }
              });
            }else{
              my.hideLoading();
              my.removeStorage({
                key: 'token',
                success: function(){
                  my.alert({
                    content: '授权登录失败',
                  });
                }
              });
            }
          }
        });
      }
    });
    my.hideLoading();
  },
  handleTap(){
    my.navigateTo({ url: '../todos/todos' });
  },
  comfirn(){
    my.showLoading({});
    var that = this;
     my.getAuthCode({
      scopes: ['auth_user'],
      success: (authcode) => {
        my.httpRequest({
          url: app.geturl+'/member/login-by-ali-sp',
          method: 'POST',
          data:{
            code:authcode.authCode
          },
          dataType: 'json',
          success: function(res) {
            console.info(res);
            if(res.data.msg=='登录成功'){
              my.setStorage({
                key: 'token',
                data: {
                  token:res.data.data.token,
                  time:600
                },
                success: function() {
                  my.hideLoading();
                  my.getAuthUserInfo({
                    success: (r) => {
                      my.setStorage({
                        key: 'memberInfo',
                        data: {
                          avatar:r.avatar,
                          nickName:r.nickName,
                        }
                      });
                    }
                  });
                  that.getAddress(res.data.data.token);
                }
              });
            }else{
              my.hideLoading();
              my.removeStorage({
                key: 'token',
                success: function(){
                  my.alert({
                    content: '授权登录失败',
                  });
                }
              });
            }
          }
        });
      }
    });
    my.hideLoading();
  },
  getAddress(token){
    var that = this;
    my.httpRequest({
      url: app.geturl+'/member-address/get-default',
      method: 'GET',
      data:{
        token:token
      },
      dataType: 'json',
      success: function(re) {
        console.info(re);
        if(re.data.data!=null){
          my.navigateTo({ url: '../add-todo/comfirn?is_cart=0&goods_id='+that.data.goods_id+':1'+'&aid='+re.data.data.member_address_id });
        }else{
          my.navigateTo({ url: '../add-todo/comfirn?is_cart=0&goods_id='+that.data.goods_id+':1'+'&aid=0' });
        }
      }
    });
  }
});
